package com.mybatisflex.demo.codegen;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.codegen.dialect.IDialect;
import com.mybatisflex.codegen.template.impl.EnjoyTemplate;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;

/**
 * @author 王帅
 * @since 2023-08-28
 */
@SuppressWarnings("all")
class Codegen {

    public DataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("12345678");
        return dataSource;
    }

    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();

        globalConfig.setAuthor("Suomm Macher");
        globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java");
        globalConfig.setBasePackage("com.test");
        globalConfig.setTablePrefix("sys_");
        globalConfig.setGenerateTable("sys_user");

        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("phonenumber");
        columnConfig.setLarge(true);

        TableConfig tableConfig = new TableConfig();
        tableConfig.setTableName("sys_user");
        tableConfig.setColumnConfig(columnConfig);

        ColumnConfig logicDeleteColumn = new ColumnConfig();
        logicDeleteColumn.setColumnName("del_flag");
        logicDeleteColumn.setLogicDelete(true);

        globalConfig.setTableConfig(tableConfig);
        globalConfig.setColumnConfig(logicDeleteColumn);

        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        globalConfig.setMapperGenerateEnable(true);

        return globalConfig;
    }

    @Test
    void testCodegen() {
        Generator generator = new Generator(dataSource(), globalConfig2(), IDialect.MYSQL);
        generator.generate();
    }

    public GlobalConfig globalConfig2() {
        GlobalConfig globalConfig = new GlobalConfig();

        globalConfig.getJavadocConfig()
                .setAuthor("Suomm Macher")
                .setTableCommentFormat(e -> e.split("，")[0].replace("表", ""));

        globalConfig.getPackageConfig()
                .setSourceDir(System.getProperty("user.dir") + "/src/test/java")
                .setBasePackage("com.test");

        ColumnConfig columnConfig = ColumnConfig.create()
                .setColumnName("phonenumber")
                .setLarge(true);

        TableConfig tableConfig = TableConfig.create()
                .setTableName("sys_user")
                .setColumnConfig(columnConfig);

        ColumnConfig logicDeleteColumn = ColumnConfig.create()
                .setColumnName("del_flag")
                .setLogicDelete(true);

        globalConfig.getStrategyConfig()
                .setTablePrefix("sys_")
                .setGenerateTable("sys_user")
                .setTableConfig(tableConfig)
                .setColumnConfig(logicDeleteColumn);

        globalConfig.enableEntity()
                .setWithLombok(true)
                .setOverwriteEnable(true);

        globalConfig.enableMapper()
                .setMapperAnnotation(true)
                .setOverwriteEnable(true);

        globalConfig.getTemplateConfig()
                .setTemplate(new EnjoyTemplate())
                .setService("/my-service.tpl");

        globalConfig.enableService();

        return globalConfig;
    }

}